package com.fling.sound;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.math.MathUtils;
import com.fling.Constants;
import com.fling.entity.Entity;
import com.fling.observer.Event;
import com.fling.observer.Observer;

public class ConsoleAudio implements IAudio, Constants, Observer {

	public float DEFAULT_VOLUME = 0.4f;
	public boolean mute;
	private ArrayList<Music> musicList;
	private Music currentMusic;
	private Map<SOUND, Sound> soundList;
	private Music MUSIC1;
	private Music MUSIC2;

	public ConsoleAudio() {
		mute = false;
		MUSIC1 = Gdx.audio.newMusic(Gdx.files.internal("data/sound/flingMusic1.mp3"));
		MUSIC2 = Gdx.audio.newMusic(Gdx.files.internal("data/sound/flingMusic2.mp3"));
		musicList = new ArrayList<Music>();
		musicList.add(MUSIC1);
		musicList.add(MUSIC2);
		currentMusic = musicList.get(MathUtils.random(0, musicList.size() - 1));
		initSounds();
	}

	private void initSounds() {
		soundList = new HashMap<SOUND, Sound>();
		soundList.put(SOUND.PASS, Gdx.audio.newSound(Gdx.files.internal(passRowSound)));
		soundList.put(SOUND.UNPASS, Gdx.audio.newSound(Gdx.files.internal(unpassRowSound)));
		soundList.put(SOUND.MENU, Gdx.audio.newSound(Gdx.files.internal(menuClick)));
		soundList.put(SOUND.COIN, Gdx.audio.newSound(Gdx.files.internal(coinSound)));
		soundList.put(SOUND.FIRST_JUMP, Gdx.audio.newSound(Gdx.files.internal(firstJumpSound)));
		soundList.put(SOUND.SECOND_JUMP, Gdx.audio.newSound(Gdx.files.internal(secondJumpSound)));
		soundList.put(SOUND.THIRD_JUMP, Gdx.audio.newSound(Gdx.files.internal(thirdJumpSound)));
		soundList.put(SOUND.RESTART, Gdx.audio.newSound(Gdx.files.internal(restartSound)));
		soundList.put(SOUND.WALL, Gdx.audio.newSound(Gdx.files.internal(wallSound)));
		soundList.put(SOUND.START, Gdx.audio.newSound(Gdx.files.internal(startSound)));
	}

	@Override
	public void pickMusic() {
		Music m = musicList.get(MathUtils.random(0, musicList.size() - 1));
		while (m == currentMusic) {
			m = musicList.get(MathUtils.random(0, musicList.size() - 1));
		}
		currentMusic = m;
		playMusic();
	}

	@Override
	public void playSound(SOUND soundID) {
		playSound(soundID, DEFAULT_VOLUME);
	}

	public void playSound(SOUND soundID, float volume) {
		if (!mute) {
			soundList.get(soundID).play(volume);
		}
	}

	@Override
	public void stopSound(SOUND soundID) {
		soundList.get(soundID).stop();
	}

	@Override
	public void stopAllSounds() {
	}

	@Override
	public void playMusic() {
		if (!mute) {
			currentMusic.play();
			currentMusic.setVolume(DEFAULT_VOLUME);
			if (currentMusic.equals(MUSIC1)) {
				currentMusic.setVolume(DEFAULT_VOLUME * 0.5f);
			}
			currentMusic.setLooping(false);
		} else {
			stopMusic();
		}
	}

	@Override
	public void stopMusic() {
		currentMusic.pause();
	}

	@Override
	public void setMusicVolume(float vol) {
		currentMusic.setVolume(vol);

	}

	@Override
	public Music getMusic() {
		return currentMusic;
	}

	@Override
	public void onNotify(Entity entity, Event event) {
		switch (event) {
		case EVENT_PASSED_BLOCK:
			playSound(SOUND.PASS);
			break;
		case EVENT_FIRST_JUMP:
			playSound(SOUND.FIRST_JUMP);
			break;
		case EVENT_SECOND_JUMP:
			playSound(SOUND.SECOND_JUMP);
			break;
		case EVENT_THIRD_JUMP:
			playSound(SOUND.THIRD_JUMP);
			break;
		case EVENT_FALL:

			break;
		case EVENT_HIT_WALL:
			playSound(SOUND.WALL);
			break;
		case EVENT_UNPASSED_BLOCK:
			playSound(SOUND.UNPASS);
			break;
		case EVENT_RESTART:
			playSound(SOUND.RESTART, DEFAULT_VOLUME * 2);
			break;
		case EVENT_GO:
			playSound(SOUND.START, DEFAULT_VOLUME * 3);
			break;
		case EVENT_COIN_GRABBED:
			playSound(SOUND.COIN);
			break;
		case EVENT_CLICK:
			playSound(SOUND.MENU);
			break;
		case EVENT_HERO_DIED:

			break;
		default:
			break;
		}
	}

	public void setMute(boolean m) {
		mute = m;
		if (mute) {
			DEFAULT_VOLUME = 0;
			currentMusic.setVolume(DEFAULT_VOLUME);
		} else {
			DEFAULT_VOLUME = 0.4f;
			currentMusic.setVolume(DEFAULT_VOLUME);
		}
	}

	@Override
	public void setSoundFXVolume(float vol) {
		// TODO Auto-generated method stub

	}

}
